//
//    Tokaplot
//    https://code.google.com/p/tokaplot/
//
//    License: GPL v3
//    https://code.google.com/p/tokaplot/wiki/License
//
//    This file is part of Tokaplot.
//
//    Tokaplot is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    Tokaplot is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with Tokaplot.  If not, see <http://www.gnu.org/licenses/>
//

package com.tokaplot;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

public class Diagnostic extends Activity
{
	private static String diagString = "";
	
    @Override
    public void onCreate(Bundle savedInstanceState)
    {    	  
        super.onCreate(savedInstanceState);
        setResult(RESULT_CANCELED); 
        setContentView(R.layout.diagnostic);
        
        // Get info from intent
		DiagnosticInfo dInfo = TokaplotService.dInfo;
        final WidgetInfo wInfo = new WidgetInfo(getIntent());
        
        SharedPreferences mCheckPrefs = getApplicationContext().getSharedPreferences("TP", Context.MODE_PRIVATE);
        
        boolean isLocked = mCheckPrefs.getBoolean("isLocked", false);
        if (isLocked) {
        	((CheckBox)findViewById(R.id.diag_lock_prefs)).setChecked(true);
        } else {
        	((CheckBox)findViewById(R.id.diag_lock_prefs)).setChecked(false);
        }
        
        int priority_Type = mCheckPrefs.getInt("priority_Type", -1);
        if (priority_Type < 0) {
        	((CheckBox)findViewById(R.id.diag_lower_priority)).setChecked(true);
        }
        else {
        	((CheckBox)findViewById(R.id.diag_lower_priority)).setChecked(false);
        }
        
        // Get destroy count
        int destroy_Count = mCheckPrefs.getInt("destroy_Count", -1);
        
        // Get diagnostic info
        diagString = "Tokaplot ";
        diagString += "\nService destroy count: " + Integer.toString(destroy_Count);
		try {
			diagString += getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
		} catch (NameNotFoundException e) {
		}
		diagString += "\nModel: ";
        
        ((TextView)findViewById(R.id.diag_device)).setText(android.os.Build.MODEL);
        ((TextView)findViewById(R.id.diag_os)).setText(android.os.Build.VERSION.RELEASE);
        diagString += android.os.Build.MODEL + "\nOS: " + android.os.Build.VERSION.RELEASE + "\n";
        
        diagString += "CPU "+ setInfoText(dInfo, (TextView)findViewById(R.id.diag_rcpu), 
        		DiagnosticInfo.DIAG_CAN_READ_CPU, 
        		DiagnosticInfo.DIAG_ERROR_FILE_OPEN_CPU,
        		DiagnosticInfo.DIAG_ERROR_FILE_READ_CPU);
        
        diagString += "CPL " + setInfoText(dInfo, (TextView)findViewById(R.id.diag_rcpl), 
        		DiagnosticInfo.DIAG_CAN_READ_CPL, 
        		DiagnosticInfo.DIAG_ERROR_FILE_OPEN_CPL,
        		DiagnosticInfo.DIAG_ERROR_FILE_READ_CPL);
        
        diagString += "NET "+ setInfoText(dInfo, (TextView)findViewById(R.id.diag_rnet), 
        		DiagnosticInfo.DIAG_CAN_READ_NET, 
        		DiagnosticInfo.DIAG_ERROR_FILE_OPEN_NET,
        		DiagnosticInfo.DIAG_ERROR_FILE_READ_NET);
        
        diagString += "MEM "+ setInfoText(dInfo, (TextView)findViewById(R.id.diag_rmem), 
        		DiagnosticInfo.DIAG_CAN_READ_MEM, 
        		DiagnosticInfo.DIAG_ERROR_FILE_OPEN_MEM,
        		DiagnosticInfo.DIAG_ERROR_FILE_READ_MEM);
        
        diagString += "CUR "+ setInfoText(dInfo, (TextView)findViewById(R.id.diag_rcur), 
        		DiagnosticInfo.DIAG_CAN_READ_CUR, 
        		DiagnosticInfo.DIAG_ERROR_FILE_OPEN_CUR,
        		DiagnosticInfo.DIAG_ERROR_FILE_READ_CUR);
        
        if (dInfo.checkFlag(DiagnosticInfo.DIAG_CAN_USE_TELEPHONY)) {
        	((TextView)findViewById(R.id.diag_rtel)).setText("yes");
        	diagString += "TEL Y\n";
        } else {
        	((TextView)findViewById(R.id.diag_rtel)).setText("no");
        	((TextView)findViewById(R.id.diag_rtel)).setTextColor(0xFFFF0000);
        	diagString += "TEL N\n";
        }
        
        if (dInfo.checkFlag(DiagnosticInfo.DIAG_USING_SENSORS)) {
        	
	        if (dInfo.checkFlag(DiagnosticInfo.DIAG_CAN_USE_ACCELEROMETER)) {
	        	((TextView)findViewById(R.id.diag_racc)).setText("yes");
	        	diagString += "ACC Y\n";
	        } else {
	        	((TextView)findViewById(R.id.diag_racc)).setText("no");
	        	((TextView)findViewById(R.id.diag_racc)).setTextColor(0xFFFF0000);
	        	diagString += "ACC N\n";
	        }
	        
	        if (dInfo.checkFlag(DiagnosticInfo.DIAG_CAN_USE_MAGNETIC_FIELD)) {
	        	((TextView)findViewById(R.id.diag_rmag)).setText("yes");
	        	diagString += "MAG Y\n";
	        } else {
	        	((TextView)findViewById(R.id.diag_rmag)).setText("no");
	        	((TextView)findViewById(R.id.diag_rmag)).setTextColor(0xFFFF0000);
	        	diagString += "MAG N\n";
	        }
	        
	        if (dInfo.checkFlag(DiagnosticInfo.DIAG_CAN_USE_LIGHT)) {
	        	((TextView)findViewById(R.id.diag_rlux)).setText("yes");
	        	diagString += "LUX Y\n";
        	} else {
	        	((TextView)findViewById(R.id.diag_rlux)).setText("no");
	        	((TextView)findViewById(R.id.diag_rlux)).setTextColor(0xFFFF0000);
	        	diagString += "LUX N\n";
	        }
        } else {
        	((TextView)findViewById(R.id.diag_racc)).setText("N/A");
        	((TextView)findViewById(R.id.diag_rmag)).setText("N/A");
        	((TextView)findViewById(R.id.diag_rlux)).setText("N/A");
        	diagString += "ACC -\n";
        	diagString += "MAG -\n";
        	diagString += "LUX -\n";
        }
        
        if (dInfo.checkFlag(DiagnosticInfo.DIAG_USING_GPS)) {
        	
	        if (dInfo.checkFlag(DiagnosticInfo.DIAG_CAN_USE_GPS_SPEED)) {
	        	((TextView)findViewById(R.id.diag_rgsp)).setText("yes");
	        	diagString += "GSP Y\n";
	        } else {
	        	((TextView)findViewById(R.id.diag_rgsp)).setText("no");
	        	((TextView)findViewById(R.id.diag_rgsp)).setTextColor(0xFFFF0000);
	        	diagString += "GSP N\n";
	        }
	        
	        if (dInfo.checkFlag(DiagnosticInfo.DIAG_CAN_USE_GPS_ALT)) {
	        	((TextView)findViewById(R.id.diag_rgal)).setText("yes");
	        	diagString += "GAL Y\n";
	        } else {
	        	((TextView)findViewById(R.id.diag_rgal)).setText("no");
	        	((TextView)findViewById(R.id.diag_rgal)).setTextColor(0xFFFF0000);
	        	diagString += "GAL N\n";
	        }
	        
        } else {
        	((TextView)findViewById(R.id.diag_rgsp)).setText("N/A");
        	((TextView)findViewById(R.id.diag_rgal)).setText("N/A");
        	diagString += "GSP -\n";
        	diagString += "GAL -\n";
        	
        }
        
        findViewById(R.id.diag_lock_prefs).setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {    
            	
            	if (((CheckBox)findViewById(R.id.diag_lock_prefs)).isChecked() == true) {
	            	CharSequence text = "To unlock, drag down the notification bar and select the Tokaplot service from the notification list.";
	            	Toast toast = Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG);
	            	toast.show();
            	}
            }
        });
        
        findViewById(R.id.diag_button_reset).setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                
    	        // Return options
                Intent widgetIntent = new Intent();
                widgetIntent.setAction(TokaplotService.ACTION_APPWIDGET_RESET);
    	    	wInfo.setIntentWithInfo(widgetIntent);
    	    	sendBroadcast(widgetIntent);
    	    	
    	    	setResult(RESULT_OK);
                finish();
            }
        });
        
        findViewById(R.id.diag_button_close).setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	
            	// Check boxes  
            	SharedPreferences mPrefs = getApplicationContext().getSharedPreferences("TP", Context.MODE_PRIVATE);
    	        SharedPreferences.Editor ed = mPrefs.edit();
    	        if (((CheckBox)findViewById(R.id.diag_clear_prefs)).isChecked() == true) {
    	        	
    	        	// Clear all preference data
    	            ed.clear();
    	        }
    	        
    	        if (((CheckBox)findViewById(R.id.diag_lower_priority)).isChecked() == true) {
    	        	
    	        	// Set priority
    	            ed.putInt("priority_Type", -1);
    	        } else {
    	        	
    	        	// Clear priority
    	        	ed.putInt("priority_Type", 1);
    	        	
    	        }
    	        
    	        if (((CheckBox)findViewById(R.id.diag_lock_prefs)).isChecked() == true) {
    	        	
        	        // Return lock
                    Intent widgetIntent = new Intent();
                    widgetIntent.setAction(TokaplotService.ACTION_APPWIDGET_LOCK);
        	    	sendBroadcast(widgetIntent);
        	    	
                    ed.putBoolean("isLocked", true);
    	        } else {
    	        	ed.putBoolean("isLocked", false);
    	        }
    	        
    	        ed.commit();
    	        
    	        setResult(RESULT_OK);
                finish();
            }
        });
        
        findViewById(R.id.diag_button_cancel).setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
		        setResult(RESULT_CANCELED);
	            finish();  
        	}
        });
        
        findViewById(R.id.diag_button_email).setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	Intent emailIntent = new Intent(Intent.ACTION_SEND);
            	emailIntent.setType("text/plain");
            	String[] address = new String[1];
            	address[0] = "conjugatedirection@gmail.com";
            	emailIntent.putExtra(Intent.EXTRA_EMAIL, address);
            	emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Tokaplot+ Diagnostics");
            	emailIntent.putExtra(Intent.EXTRA_TEXT, diagString);
            	Diagnostic.this.startActivity(Intent.createChooser(emailIntent, "Send email..."));
            }
        });
    }
    
    String setInfoText(DiagnosticInfo dInfo, TextView vText, int dFlag, int dErrorOpen, int dErrorRead) {
    	
    	// Set info text in text view
    	String rString = "";
        if (dInfo.checkFlag(dFlag)) {
        	vText.setText("yes");
        	rString = "Y\n";
    	} else {
        	if (dInfo.checkError(dErrorOpen)) {
        		vText.setText("no (information not found)");
        		rString = "N(1)\n";
        	} else if (dInfo.checkError(dErrorRead)) {
        		vText.setText("no (cannot read file)");
        		rString = "N(2)\n";
        	} else {
        		vText.setText("no (unknown reason!)");
        		rString = "N(?)\n";
        	}
        	vText.setTextColor(0xFFFF0000);
        }
        return rString;
    }
}
